home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / sort.cc < prev    next >
C/C++ Source or Header  |  1996-11-03  |  7KB  |  383 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "defun-dld.h"
  28. #include "error.h"
  29. #include "gripes.h"
  30. #include "help.h"
  31. #include "oct-obj.h"
  32.  
  33. // This is algorithm 5.2.4L from Knuth, Volume 3.
  34.  
  35. // XXX FIXME XXX -- there is way too much duplicated code here given
  36. // that the sort algorithms are all the same, and only the type of the
  37. // data and the comparison changes...
  38. //
  39. // Maybe some cpp abuse will make it better.
  40.  
  41. static Array<int>
  42. create_index_array (int n)
  43. {
  44.   Array<int> l (n+2);
  45.  
  46.   l (0) = 1;
  47.  
  48.   for (int i = 1; i < n - 1; i++)
  49.     l (i) = -(i+2);
  50.  
  51.   l (n-1) = 0;
  52.   l (n) = 0;
  53.   l (n+1) = 2;
  54.  
  55.   return l;
  56. }
  57.  
  58. #define SORT_INIT_PHASE(n) \
  59.   int s = 0; \
  60.   int t = n + 1; \
  61.   int p = l (s); \
  62.   int q = l (t); \
  63.   if (q == 0) \
  64.      break
  65.  
  66. #define SORT_COMMON_CODE \
  67.   p = -p; \
  68.   q = -q; \
  69.   if (q == 0) \
  70.     { \
  71.       l (s) = (l (s) < 0) \
  72.     ? ((p < 0) ? p : -p) \
  73.       : ((p >= 0) ? p : -p); \
  74.       l (t) = 0; \
  75.       break; \
  76.     } \
  77.  
  78. #define SORT_REORDER_PHASE_ONE \
  79.   l (s) = (l (s) < 0) \
  80.     ? ((q < 0) ? q : -q) \
  81.       : ((q >= 0) ? q : -q); \
  82.   s = q; \
  83.   q = l (q); \
  84.   if (q <= 0) \
  85.     { \
  86.       l (s) = p; \
  87.       s = t; \
  88.       do \
  89.     { \
  90.       t = p; \
  91.       p = l (p); \
  92.     } \
  93.       while (p > 0); \
  94.       SORT_COMMON_CODE; \
  95.     } \
  96.  
  97. #define SORT_REORDER_PHASE_TWO \
  98.   l (s) = (l (s) < 0) \
  99.     ? ((p < 0) ? p : -p) \
  100.       : ((p >= 0) ? p : -p); \
  101.   s = p; \
  102.   p = l (p); \
  103.   if (p <= 0) \
  104.     { \
  105.       l (s) = q; \
  106.       s = t; \
  107.       do \
  108.     { \
  109.       t = q; \
  110.       q = l (q); \
  111.     } \
  112.       while (q > 0); \
  113.       SORT_COMMON_CODE; \
  114.     }
  115.  
  116. #define DO_SORT(n, condition) \
  117.   while (1) \
  118.     { \
  119.       SORT_INIT_PHASE(n); \
  120.       while (1) \
  121.     { \
  122.       if (condition) \
  123.         { \
  124.           SORT_REORDER_PHASE_ONE; \
  125.         } \
  126.       else \
  127.         { \
  128.           SORT_REORDER_PHASE_TWO; \
  129.         } \
  130.     } \
  131.     }
  132.  
  133. #define VECTOR_CREATE_RETURN_VALUES(vs, v) \
  134.   int k = l (0); \
  135.   idx (0) = k; \
  136.   vs (0) = v (k-1); \
  137.   for (int i = 1; i < n; i++) \
  138.     { \
  139.       k = l ((int) idx (i-1)); \
  140.       idx (i) = k; \
  141.       vs (i) = v (k-1); \
  142.     }
  143.  
  144. #define MATRIX_CREATE_RETURN_VALUES(ms, m) \
  145.   int k = l (0); \
  146.   idx (0, j) = k; \
  147.   ms (0, j) = m (k-1, j); \
  148.   for (int i = 1; i < nr; i++) \
  149.     { \
  150.       k = l ((int) idx (i-1, j)); \
  151.       idx (i, j) = k; \
  152.       ms (i, j) = m (k-1, j); \
  153.     }
  154.  
  155. static octave_value_list
  156. mx_sort (const Matrix& m)
  157. {
  158.   octave_value_list retval;
  159.  
  160.   int nr = m.rows ();
  161.   int nc = m.columns ();
  162.  
  163.   Matrix ms (nr, nc);
  164.   Matrix idx (nr, nc);
  165.  
  166.   if (nr == 1 && nc > 0)
  167.     {
  168.       retval (1) = Matrix (nr, nc, 1.0);
  169.       retval (0) = m;
  170.  
  171.       return retval;
  172.     }
  173.   else if (nr > 1 && nc > 0)
  174.     {
  175.       for (int j = 0; j < nc; j++)
  176.     {
  177.       Array<int> l = create_index_array (nr);
  178.  
  179.       DO_SORT (nr, (m (p-1, j) > m (q-1, j)));
  180.  
  181.       MATRIX_CREATE_RETURN_VALUES (ms, m);
  182.     }
  183.     }
  184.  
  185.   retval (1) = idx;
  186.   retval (0) = ms;
  187.  
  188.   return retval;
  189. }
  190.  
  191. static octave_value_list
  192. mx_sort (const RowVector& v)
  193. {
  194.   octave_value_list retval;
  195.  
  196.   int n = v.capacity ();
  197.  
  198.   RowVector vs (n);
  199.   RowVector idx (n);
  200.  
  201.   if (n == 1)
  202.     {
  203.       retval (1) = RowVector (n, 1.0);
  204.       retval (0) = v;
  205.  
  206.       return retval;
  207.     }
  208.   else if (n > 1)
  209.     {
  210.       Array<int> l = create_index_array (n);
  211.  
  212.       DO_SORT (n, (v (p-1) > v (q-1)));
  213.  
  214.       VECTOR_CREATE_RETURN_VALUES (vs, v);
  215.     }
  216.  
  217.   retval (1) = octave_value (idx, 0);
  218.   retval (0) = octave_value (vs, 0);
  219.  
  220.   return retval;
  221. }
  222.  
  223. static octave_value_list
  224. mx_sort (const ComplexMatrix& cm)
  225. {
  226.   octave_value_list retval;
  227.  
  228.   int nr = cm.rows ();
  229.   int nc = cm.columns ();
  230.  
  231.   ComplexMatrix cms (nr, nc);
  232.   Matrix idx (nr, nc);
  233.  
  234.   if (nr == 1 && nc > 0)
  235.     {
  236.       retval (1) = Matrix (nr, nc, 1.0);
  237.       retval (0) = cm;
  238.  
  239.       return retval;
  240.     }
  241.   else if (nr > 1 && nc > 0)
  242.     {
  243.       for (int j = 0; j < nc; j++)
  244.     {
  245.       Array<int> l = create_index_array (nr);
  246.  
  247.       int all_elts_real = 1;
  248.       for (int i = 0; i < nr; i++)
  249.         if (imag (cm (i, j)) != 0.0)
  250.           {
  251.         all_elts_real = 0;
  252.         break;
  253.           }
  254.  
  255.       DO_SORT (nr, ((all_elts_real
  256.              && real (cm (p-1, j)) > real (cm (q-1, j)))
  257.             || abs (cm (p-1, j)) > abs (cm (q-1, j))));
  258.  
  259.       MATRIX_CREATE_RETURN_VALUES (cms, cm);
  260.     }
  261.     }
  262.  
  263.   retval (1) = idx;
  264.   retval (0) = cms;
  265.  
  266.   return retval;
  267. }
  268.  
  269. static octave_value_list
  270. mx_sort (ComplexRowVector& cv)
  271. {
  272.   octave_value_list retval;
  273.  
  274.   int n = cv.capacity ();
  275.  
  276.   ComplexRowVector cvs (n);
  277.   RowVector idx (n);
  278.  
  279.   if (n == 1)
  280.     {
  281.       retval (1) = RowVector (n, 1.0);
  282.       retval (0) = cv;
  283.  
  284.       return retval;
  285.     }
  286.   else if (n > 1)
  287.     {
  288.       Array<int> l = create_index_array (n);
  289.  
  290.       int all_elts_real = 1;
  291.       for (int i = 0; i < n; i++)
  292.     if (imag (cv (i)) != 0.0)
  293.       {
  294.         all_elts_real = 0;
  295.         break;
  296.       }
  297.  
  298.       DO_SORT (n, ((all_elts_real
  299.             && real (cv (p-1)) > real (cv (q-1)))
  300.            || abs (cv (p-1)) > abs (cv (q-1))));
  301.  
  302.       VECTOR_CREATE_RETURN_VALUES (cvs, cv);
  303.     }
  304.  
  305.   retval (1) = octave_value (idx, 0);
  306.   retval (0) = octave_value (cvs, 0);
  307.  
  308.   return retval;
  309. }
  310.  
  311. DEFUN_DLD (sort, args, nargout,
  312.   "[S, I] = sort (X)\n\
  313. \n\
  314. sort the columns of X, optionally return sort index")
  315. {
  316.   octave_value_list retval;
  317.  
  318.   int nargin = args.length ();
  319.  
  320.   if (nargin != 1)
  321.     {
  322.       print_usage ("sort");
  323.       return retval;
  324.     }
  325.  
  326.   int return_idx = nargout > 1;
  327.   if (return_idx)
  328.     retval.resize (2);
  329.   else
  330.     retval.resize (1);
  331.  
  332.   octave_value arg = args(0);
  333.  
  334.   if (arg.is_real_type ())
  335.     {
  336.       Matrix m = arg.matrix_value ();
  337.  
  338.       if (! error_state)
  339.     {
  340.       if (m.rows () == 1)
  341.         {
  342.           int nc = m.columns ();
  343.           RowVector v (nc);
  344.           for (int i = 0; i < nc; i++)
  345.         v (i) = m (0, i);
  346.  
  347.           retval = mx_sort (v);
  348.         }
  349.       else
  350.         retval = mx_sort (m);
  351.     }
  352.     }
  353.   else if (arg.is_complex_type ())
  354.     {
  355.       ComplexMatrix cm = arg.complex_matrix_value ();
  356.  
  357.       if (! error_state)
  358.     {
  359.       if (cm.rows () == 1)
  360.         {
  361.           int nc = cm.columns ();
  362.           ComplexRowVector cv (nc);
  363.           for (int i = 0; i < nc; i++)
  364.         cv (i) = cm (0, i);
  365.  
  366.           retval = mx_sort (cv);
  367.         }
  368.       else
  369.         retval = mx_sort (cm);
  370.     }
  371.     }
  372.   else
  373.     gripe_wrong_type_arg ("sort", arg);
  374.  
  375.   return retval;
  376. }
  377.  
  378. /*
  379. ;;; Local Variables: ***
  380. ;;; mode: C++ ***
  381. ;;; End: ***
  382. */
  383.